package com.yzh.zhipu.executor.impl;

import com.alibaba.fastjson2.JSON;
import com.yzh.zhipu.ChatGlmApi;
import com.yzh.zhipu.executor.Executor;
import com.yzh.zhipu.model.ChatRequest;
import com.yzh.zhipu.model.ChatResponse;
import com.yzh.zhipu.model.ImageRequest;
import com.yzh.zhipu.model.ImageResponse;
import com.yzh.zhipu.model.vo.EventType;
import com.yzh.zhipu.session.Configuration;
import okhttp3.*;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

public class GlmOldExecutor implements Executor {
    private Configuration configuration;
    private EventSource.Factory factory;
    
    public GlmOldExecutor(Configuration configuration) {
        this.configuration = configuration;
        this.factory = configuration.createRequestFactory();
    }
    @Override
    public EventSource completions(ChatRequest chatRequest, EventSourceListener eventSourceListener) {
        Request request = new Request.Builder()
                .url(configuration.getApiHost().concat(ChatGlmApi.v3_chat))
                .post(RequestBody.create(MediaType.parse("application/json"), chatRequest.toString()))
                .build();
        return factory.newEventSource(request, eventSourceListener);
    }

    @Override
    public ChatResponse completionsSync(ChatRequest chatRequest) throws IOException {
        Request request = new Request.Builder()
                .url(configuration.getApiHost().concat(ChatGlmApi.v3_chat))
                .post(RequestBody.create(MediaType.parse("application/json"), chatRequest.toString()))
                .build();
        OkHttpClient okHttpClient = configuration.getOkHttpClient();
        Response response = okHttpClient.newCall(request).execute();
        if (!response.isSuccessful()) {
            throw new RuntimeException("请求失败: "+ JSON.toJSONString(response));
        }
        return JSON.parseObject(response.body().string(), ChatResponse.class);
    }

    @Override
    public CompletableFuture<java.lang.String> completions(ChatRequest chatRequest) {
        // 构建请求信息
        Request request = new Request.Builder()
                .url(configuration.getApiHost().concat(ChatGlmApi.v3_chat))
                .post(RequestBody.create(MediaType.parse("application/json"), chatRequest.toString()))
                .build();
        
        StringBuilder sb = new StringBuilder();
        
        CompletableFuture future = new CompletableFuture();
        factory.newEventSource(request, new EventSourceListener(){
            @Override
            public void onEvent(EventSource eventSource, @Nullable java.lang.String id, @Nullable java.lang.String type, java.lang.String data) {
                ChatResponse chatResponse = JSON.parseObject(data, ChatResponse.class);
                sb.append(chatResponse.getData());
                if (EventType.finish.getCode().equals(type)) {
                    future.complete(sb.toString());
                }
            }
        });
        return future;
    }

    @Override
    public ImageResponse genImage(ImageRequest request) {
        throw new RuntimeException("旧版接口无图生成功能");
    }
}
